package com.example.androidshixi;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private boolean isAdminMode = false;
    private static final int REQUEST_CODE_SETTINGS = 100;
    private static final int REQUEST_CODE_ADD_ARTICLE = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = findViewById(R.id.bottom_navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment selectedFragment = null;
            int itemId = item.getItemId();
            if (itemId == R.id.headlines_item) {
                selectedFragment = new HeadlinesFragment();
            } else if (itemId == R.id.mall_item) {
                selectedFragment = new MallFragment();
            } else if (itemId == R.id.my_item) {
                selectedFragment = new MyFragment();
            }
            if (selectedFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, selectedFragment);
                transaction.commit();
            }
            return true;
        });

        // 检查是否携带了显示商城界面的标识
        boolean showMall = getIntent().getBooleanExtra("show_mall", false);
        if (showMall) {
            // 显示商城界面
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, new MallFragment());
            transaction.commit();
            // 设置底部导航栏选中商城项
            bottomNavigationView.setSelectedItemId(R.id.mall_item);
        } else {
            // 默认显示头条页面
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, new HeadlinesFragment());
            transaction.commit();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_SETTINGS) {
            if (resultCode == RESULT_OK) {
                // 处理管理员模式切换
                isAdminMode = data.getBooleanExtra("admin_mode", false);
                updateFragmentsAdminMode();
            } else if (resultCode == RESULT_FIRST_USER) {
                // 处理新添加的文章
                if (data != null) {
                    Article newArticle = data.getParcelableExtra("new_article");
                    if (newArticle != null) {
                        updateHeadlinesFragmentWithNewArticle(newArticle);
                    }
                }
            }
        }
    }

    private void updateFragmentsAdminMode() {
        Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (currentFragment instanceof HeadlinesFragment) {
            ((HeadlinesFragment) currentFragment).setAdminMode(isAdminMode);
        } else if (currentFragment instanceof MallFragment) {
            ((MallFragment) currentFragment).setAdminMode(isAdminMode);
        }
    }

    private void updateHeadlinesFragmentWithNewArticle(Article newArticle) {
        Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (currentFragment instanceof HeadlinesFragment) {
            ((HeadlinesFragment) currentFragment).addNewArticle(newArticle);
        }
    }

    // 在启动SettingsActivity时使用这个方法
    public void startSettingsActivityForResult() {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivityForResult(intent, REQUEST_CODE_SETTINGS);
    }
}